package com.qckj.amz.domain.spapi.feeds.home.InventoryFeedXml;

import com.qckj.amz.domain.spapi.feeds.home.AmazonEnvelope;
import com.qckj.amz.domain.spapi.feeds.home.Header;
import com.qckj.amz.domain.spapi.feeds.home.Message;
import org.apache.commons.lang3.StringUtils;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

/**
 <?xml version="1.0" encoding="UTF-8"?>
 <AmazonEnvelope xsi:noNamespaceSchemaLocation="amzn-envelope.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <Header>
 <DocumentVersion>1.01</DocumentVersion>
 <MerchantIdentifier>Q_M_STORE_123</MerchantIdentifier>
 </Header>
 <MessageType>Price</MessageType>
 <Message>
 <MessageID>1</MessageID>
 <OperationType>Update</OperationType>
 <Price>
 <SKU>11400400</SKU>
 <StandardPrice currency="CNY">119.00</StandardPrice>
 <Sale>
 <StartDate>2011-12-12T00:00:00.000Z</StartDate>
 <EndDate>2099-06-12T00:00:00.000Z</EndDate>
 <SalePrice currency="CNY">95.00</SalePrice>
 </Sale>
 </Price>
 </Message>
 </AmazonEnvelope>

 */

/**
 * Home类目，Java对象转xml字符串
 */
public class TestCreateXml {
    public static void main(String[] args) throws JAXBException {

        Inventory inventory = new Inventory();
        inventory.setSKU("SKU123456");
        inventory.setQuantity("30");
        inventory.setFulfillmentLatency("3");//完全延迟，具体属性不确定，百度查到这个一个延迟发货意思，1-30整数，默认填写3


        /**
         * 二级/Message,此节点为List对象，可以包含主体、变体
         */
        List<Message> messageList = new ArrayList<>();
        Message message = new Message();
        message.setMessageID(1L);
        message.setOperationType("Update");
        message.setInventory(inventory);
        messageList.add(message);

        /**
         * 二级/Header
         */
        Header header = new Header();
        header.setDocumentVersion("1.01");
        header.setMerchantIdentifier("ARBGXXE1RPNJC");

        /**
         * 一级/根节点AmazonEnvelope
         */
        AmazonEnvelope ae = new AmazonEnvelope();
        ae.setHeader(header);
        ae.setMessageType("Price");
        ae.setMessage(messageList);


        /**
         * 实现java对象转xml字符串
         */
        JAXBContext jaxbContext = JAXBContext.newInstance(AmazonEnvelope.class);
        StringWriter writer = new StringWriter();
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.marshal(ae, writer);
        String xmlStr = writer.toString();
        xmlStr = StringUtils.replace(xmlStr, "&quot;", "'");

        System.out.println(xmlStr);


    }
}
